package com.example.administrator.yuejian35.Recommend.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian35.Club.bean.Course;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.Recommend.bean.Applyresult;
import com.example.administrator.yuejian35.Recommend.model.RecommendModel;
import com.example.administrator.yuejian35.base.BaseActivity;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.administrator.yuejian35.Config.Img_URL;

public class CourseApplyActivity extends AppCompatActivity {
    private String courseId;
    private TextView course_name;
    private TextView course_type;
    private TextView uname;
    private EditText edit_phone;
    private EditText edt;
    private String uid;
    private Button apply_button;
    private TextView price;
    private TextView subbt;
    private TextView addbt;
    private TextView course_notice;
    private TextView course_price;
    private int getnum=0;
    private String snum;
   private ImageView ic_back;
    private ImageView coursepic;
    private int sum=0;
    private  int p;
    private String gettel;
    private int num = 0;

    public CourseApplyActivity() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_apply);

        courseId = getIntent().getStringExtra("courseid");
        course_name = (TextView) findViewById(R.id.course_name);
        course_price = (TextView) findViewById(R.id.course_price);
        course_type = (TextView) findViewById(R.id.course_type);
        price = (TextView) findViewById(R.id.price);
        subbt = (TextView) findViewById(R.id.subbt);
        uname = (TextView) findViewById(R.id.uname);
        addbt = (TextView) findViewById(R.id.addbt);
        ic_back=(ImageView) findViewById(R.id.video_pic);
        coursepic=(ImageView) findViewById(R.id.coursepic);
        course_notice = (TextView) findViewById(R.id.course_notice);
        edit_phone = (EditText) findViewById(R.id.edit_phone);
        edt = (EditText) findViewById(R.id.edt);
        apply_button = (Button) findViewById(R.id.apply_button);
        addbt.setTag("-");
        subbt.setTag("+");
        edt.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
        edt.setText(String.valueOf(num));
        SetViewListener();
        //Toast.makeText(CourseApplyActivity.this,courseId,Toast.LENGTH_SHORT).show();

        ic_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        RecommendModel recommendmodel = new RecommendModel();
        Call call = recommendmodel.getcourse(courseId);
        call.enqueue(new Callback<Course>() {
            @Override
            public void onResponse(Call<Course> call, Response<Course> response) {
                course_name.setText(response.body().getCoursename());
                if (response.body().getDegree().equals("1")) {
                    course_type.setText("菜鸟级");
                } else if (response.body().getDegree().equals("3")) {
                    course_type.setText("进阶级");
                } else {
                    course_type.setText("入门级");
                }
                course_name.setText(response.body().getCoursename());
                course_notice.setText(response.body().getBmnotice());
                uname.setText(BaseActivity.getUserName());
                Picasso.with(CourseApplyActivity.this)
                        .load(Img_URL+response.body().getCoursepic())
                        .into(coursepic);
                // course_time.setText("上课时间："+response.body().getCoursetime());
                course_price.setText(response.body().getPrice() + "元/人");
                p = Integer.parseInt(response.body().getPrice().toString());


            }

            @Override
            public void onFailure(Call<Course> call, Throwable t) {

            }
        });


        apply_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                gettel=edit_phone.getText().toString();
                uid= BaseActivity.getUserId();
                snum= String.valueOf (getnum);
                if(gettel.equals("")){
                    Toast.makeText(CourseApplyActivity.this,"请填写电话号码", Toast.LENGTH_SHORT).show();
                }
                else if(snum.equals("0"))
                {
                  // Toast.makeText(CourseApplyActivity.this,gettel,Toast.LENGTH_SHORT).show();
                    Toast.makeText(CourseApplyActivity.this,"请选择购买数量", Toast.LENGTH_SHORT).show();
                }
                else{
                    RecommendModel recommendmodel = new RecommendModel();
                    Call call1 = recommendmodel.applycourse(courseId,uid,snum,gettel);
                    call1.enqueue(new Callback<Applyresult>() {
                        @Override
                        public void onResponse(Call<Applyresult> call, Response<Applyresult> response) {
                            Toast.makeText(CourseApplyActivity.this,"报名成功！", Toast.LENGTH_SHORT).show();
                            finish();
                        }

                        @Override
                        public void onFailure(Call<Applyresult> call, Throwable t) {

                        }
                    });
                }

            }
        });

    }


    private void SetViewListener() {
        addbt.setOnClickListener(new OnButtonClickListener());
        subbt.setOnClickListener(new OnButtonClickListener());
        edt.addTextChangedListener(new OnTextChangeListener());
    }



    class OnButtonClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            String numString = edt.getText().toString();
            if (numString == null || numString.equals("")) {
                num = 0;
                edt.setText("0");
            } else {
                if (v.getTag().equals("-")) {
                    if (++num < 0) //先加，再判断
                    {
                        num--;
                        Toast.makeText(CourseApplyActivity.this, "请输入一个大于0的数字", Toast.LENGTH_SHORT).show();
                    } else {
                        edt.setText(String.valueOf(num));
                    }
                } else if (v.getTag().equals("+")) {
                    if (--num < 0) //先减，再判断
                    {
                        num++;
                        Toast.makeText(CourseApplyActivity.this, "请输入一个大于0的数字", Toast.LENGTH_SHORT).show();
                    } else {
                        edt.setText(String.valueOf(num));
                    }
                }
            }
            getnum= Integer.parseInt(edt.getText().toString());
            sum=p*getnum;
            price.setText("共计："+sum+"元");
         //   Toast.makeText(CourseApplyActivity.this,sum+"",Toast.LENGTH_SHORT).show();
        }
    }
    class OnTextChangeListener implements TextWatcher
    {
        @Override
        public void afterTextChanged(Editable s)
        {
            String numString = s.toString();
            if(numString == null || numString.equals(""))
            {
                num = 0;
                }
            else {
                int numInt = Integer.parseInt(numString);
                if (numInt < 0)
                {
                    Toast.makeText(CourseApplyActivity.this, "请输入一个大于0的数字", Toast.LENGTH_SHORT).show();
                    } else
                {
                    //设置EditText光标位置 为文本末端
                    edt.setSelection(edt.getText().toString().length());
                    num = numInt;
                    }
                }
            }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after)
        {
            }
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count)
        {
            }
    }

}











